// pages/order/orderConfirm/orderConfirm.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        type: null,
        userInfo: null,
        orderInfo: null,
        orderMoney: 0,
        orderPostage: 0,
        orderMark: '',
        address: null,
        payMethods: 1,
        isLoading: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            type: options.type,
            orderInfo: wx.getStorageSync('orderInfo')
        })
        this.getUserInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        if (this.data.type != 'point') {
            this.getAddress()
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {
        wx.removeStorageSync('orderInfo')
        wx.removeStorageSync('selectAddress')
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    getUserInfo: function() {
        const that = this
        let data = {}
        common.post('User/user_info', data).then(function(res) {
            if (res.code == 204) {
                wx.showToast({
                    title: res.error,
                    icon: 'none',
                    duration: 2000
                })
                return
            }
            that.setData({
                userInfo: res
            })
            that.getOrderInfo()
        }).catch(function(e) {})
    },

    getOrderInfo: function() {
        const that = this
        let orderPostage = this.data.orderPostage
        let orderMoney = this.data.orderMoney
        this.data.orderInfo.cartSelectList.forEach((items) => {
            items.goods_sku.forEach((item) => {
                if (that.data.userInfo.role_type == 0) {
                    orderMoney += item.price * item.num
                } else if (that.data.userInfo.role_type == 1) {
                    orderMoney += item.retail_price * item.num
                }
                that.setData({
                    orderMoney: orderMoney
                })
            })
        })
        if (this.data.type == 'share' || this.data.type == 'new') {
            this.data.orderInfo.cartSelectList.forEach((items) => {
                items.goods_sku.forEach((item) => {
                    orderPostage += item.extra_money
                    that.setData({
                        orderPostage: orderPostage
                    })
                })
            })
        }
    },

    getAddress() {
        const that = this
        if (wx.getStorageSync('selectAddress')) {
            let address = wx.getStorageSync('selectAddress')
            that.setData({
                address: address
            })
        } else {
            let data = {}
            common.post('Address/address_list', data).then(function(res) {
                if (res.code == 204) {
                    wx.showToast({
                        title: res.error,
                        icon: 'none',
                        duration: 1000
                    })
                } else {
                    res.forEach(function(item) {
                        if (item.default_id == 2) {
                            that.setData({
                                address: item
                            })
                        }
                    })
                }
            }).catch(function(e) {})
        }
    },

    goselectAddress: function() {
        wx.navigateTo({
            url: '/pages/user/address/addressList/addressList?type=select'
        })
    },

    orderMarkInput: function(e) {
        this.setData({
            orderMark: e.detail.value
        })
    },

    payChange: function(e) {
        this.setData({
            payMethods: e.detail.value
        })
    },

    goPay: function() {
        const that = this
        if (this.data.address == null) {
            wx.showToast({
                title: '请选择收货地址',
                icon: 'none',
                duration: 2000
            })
            return
        }
        if (this.data.isLoading) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.login({
            success(response) {
                if (response.code) {
                    //发起网络请求
                    let data
                    if (that.data.type == 'normal') {
                        data = {
                            store_id: wx.getStorageSync('storeId'),
                            address_id: that.data.address.address_id,
                            pay_id: that.data.payMethods,
                            total_price: that.data.orderMoney + that.data.orderPostage,
                            breft: that.data.orderMark,
                            pay_client: 1,
                            cart_id_list: that.data.orderInfo.cartIdList,
                            is_goods: that.data.orderInfo.is_goods,
                            code: response.code
                        }
                    }
                    if (that.data.payMethods == 1) {
                        common.post('Order/new_order', data).then(function(response) {
                            if (response.code == 204) {
                                wx.showToast({
                                    title: response.error,
                                    icon: 'none',
                                    duration: 2000
                                })
                                that.setData({
                                    isLoading: false
                                })
                                return
                            }
                            wx.requestPayment({
                                timeStamp: response.timeStamp,
                                nonceStr: response.nonceStr,
                                package: response.package,
                                signType: response.signType,
                                paySign: response.paySign,
                                success(res) {
                                    that.setData({
                                        isLoading: false
                                    })
                                    wx.redirectTo({
                                        url: '/pages/order/orderCenter/orderCenter?orderType=2'
                                    })
                                },
                                fail(res) {
                                    that.setData({
                                        isLoading: false
                                    })
                                    wx.showToast({
                                        title: '支付失败',
                                        icon: 'none',
                                        duration: 1000
                                    })
                                    wx.redirectTo({
                                        url: '/pages/order/orderCenter/orderCenter?orderType=1'
                                    })
                                }
                            })
                        }).catch(function(e) {})
                    } else if (that.data.payMethods == 3) {
                        common.post('Order/new_order', data).then(function(response) {
                            if (response.code == 204) {
                                wx.showToast({
                                    title: response.error,
                                    icon: 'none',
                                    duration: 2000
                                })
                                that.setData({
                                    isLoading: false
                                })
                                return
                            }
                            wx.showModal({
                                title: '提示',
                                content: '零钱支付成功',
                                showCancel: false,
                                success(res) {
                                    if (res.confirm) {
                                        that.setData({
                                            isLoading: false
                                        })
                                        wx.redirectTo({
                                            url: '/pages/order/orderCenter/orderCenter?orderType=2'
                                        })
                                    }
                                }
                            })
                        }).catch(function(e) {})
                    }
                }
            }
        })
    }
})